<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 镜像列表
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                        type="primary"
                        icon="el-icon-delete"
                        class="handle-del mr10"
                >批量删除</el-button>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header">
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column label="ID" width="200" align="center">
                    <template v-slot="scope">
                        <span>{{rowId(scope.row)}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="Image" label="Image"></el-table-column>
                <el-table-column prop="State" label="State"></el-table-column>
                <el-table-column label="操作">
                    <template v-slot="scope">
                        <el-button @click="containerRemove(scope.row)">删除</el-button>
                        <el-button v-if="scope.row.State == 'exited' || scope.row.State == 'created' " @click="containerStart(scope.row)" type="primary"
                                   >启动</el-button>
                        <el-button v-if="scope.row.State == 'running'" @click="containerStop(scope.row)" type="primary"
                                   >停止</el-button>
                        <el-button v-if="scope.row.State == 'running'" @click="containerRestart(scope.row)" type="primary"
                        >重启</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
    </div>
</template>

<script>
    import axios from 'axios'
    export default {
        name: "docker_container",
        data(){
            return{
                tableData: [],
                containerListTmp:{},
            }
        },
        created() {
            this.getContainer()
        },
        methods: {
            getContainer(){
                axios.defaults.baseURL = '//127.0.0.1:8081';
                axios.get('/docker/ContainerList')
                    .then((response)=>{
                        if(response.data.code == 0){
                            this.tableData = response.data.data
                        }
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            },
            repositoryOne(row){
                let arr = row["RepoTags"][0].split(":")
                return arr[0]
            },
            rowTag(row){
                let arr = row["RepoTags"][0].split(":")
                return arr[1]
            },
            rowId(row){
                return row["Id"].slice(0,12)
            },
            containerRemove(row){
                axios.get('/docker/ContainerRemove',{
                    params: {
                        containerId: row["Id"].slice(0,12)
                    }
                }).then((response)=>{
                    if(response.data.code == 0){
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getContainer()
                    }else {
                        this.$message.error(response.data.message);
                    }
                })
                    .catch(function (error) {
                        console.log(error);
                    });
            },
            containerStart(row){
                axios.get('/docker/ContainerStart',{
                    params: {
                        containerId: row["Id"].slice(0,12)
                    }
                }).then((response)=>{
                    if(response.data.code == 0){
                        this.$message({
                            message: '启动成功',
                            type: 'success'
                        });
                        this.getContainer()
                    }else {
                        this.$message.error(response.data.message);
                    }
                }).catch(function (error) {
                        console.log(error);
                });
            },
            containerStop(row){
                axios.get('/docker/ContainerStop',{
                    params: {
                        containerId: row["Id"].slice(0,12)
                    }
                }).then((response)=>{
                    if(response.data.code == 0){
                        this.$message({
                            message: '停止成功',
                            type: 'success'
                        });
                        this.getContainer()
                    }else {
                        this.$message.error(response.data.message);
                    }
                }).catch(function (error) {
                    console.log(error);
                });
            },
            containerRestart(row){
                axios.get('/docker/ContainerRestart',{
                    params: {
                        containerId: row["Id"].slice(0,12)
                    }
                }).then((response)=>{
                    if(response.data.code == 0){
                        this.$message({
                            message: '重启成功',
                            type: 'success'
                        });
                        this.getContainer()
                    }else {
                        this.$message.error(response.data.message);
                    }
                }).catch(function (error) {
                    console.log(error);
                });
            }
        }
    }
</script>

<style scoped>

</style>